package tp.pr3;



import java.io.FileInputStream;
import java.io.IOException;

import tp.pr3.City;
import tp.pr3.Direction;
import tp.pr3.RobotEngine;
import tp.pr3.cityLoader.*;



public class Main {

public static void main(String[] args) {
	
			if(args.length!=1)
				{
				System.err.println("Bad params.Usage: java tp.pr3.Main <mapfile><mapfile> : file with the description of the city.");
				System.exit(1);
				}
			
	    	String fileName = null;
	    	fileName="c:/mapa/madrid.txt";
	        fileName=args[0];
	    	
	    	CityLoaderFromTxtFile cityloader = new CityLoaderFromTxtFile();	 					    	
	    	
	    	try{	    		        	
	    		City city = loadFile(fileName,cityloader);
	    		if(city==null)
	    			throw new NullPointerException();
	        	RobotEngine engine = 
	        			new RobotEngine( city, cityloader.getInitialPlace(),Direction.NORTH);
				engine.startEngine();
	    		}
	    	catch(Exception e)
	    	{
	    		System.err.println(e.getMessage());
	    		System.exit(2);	
	    	}	    	
		}

		
	    private static City loadFile(String name,CityLoaderFromTxtFile cityloader) throws IOException{	
	    	City city = null;
			FileInputStream file = null;
			
			try {			
			 file = new FileInputStream(name);			 
			 city = cityloader.loadCity(file);
			 file.close();		
			}catch(Exception e)
			{
				throw new IOException("Error reading the map file: "+name+" (No existe el fichero o el directorio)\n");
			}
			return city;
	    }   

	}
		

